#include "transport_protocol.h"

//创建一个train对象
train * train_init(){
    train * t = (train *)calloc(1,sizeof(train));
    t->length = TRANS_DATA_SIZE;
    void * dat = (void *)calloc(TRANS_DATA_SIZE,sizeof(char));
    t->data = dat;
    t->type = 0;
    return t;
}
//回收 train对象的堆内存
void * train_free(train * t){
    free(t->data);
    free(t);
}

//接收和发送
void send_t(int fd,task_type type,const void * data){
    train * t = train_init();
    t->type = type;
    t->data = data;
    int len = send(fd,tra,sizeof(train),MSG_WAITALL);
    if(-1 == len )
        error_log("send_t::send");
    if(sizeof(train) > len)
        error_log("send_t::send: not send all.");
    train_free(t);
}
train * recv_t(int fd){
    train * t = train_init();
    int len = recv(fd,t,sizeof(train),MSG_WAITALL);
    if (-1 == len)
        error_log("recv_t::recv");
    if (sizeof(train) > len)
        error_log("recv_t::recv:not recv all.");
    return t;
}
//传输错误的通知信息
void exit_error(int fd,const void * err){
    recv_t(fd,ERROR_EXIT,err);
}